﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using FeedAntGame.Control;
using Microsoft.Xna.Framework.Input;
using FeedAntGame.Sprite.Combo;

namespace FeedAntGame.Sprite.Combo
{
    class EditTextSprite : ComboBaseSprite
    {
        
        Pointer pointer;
        string value = "";
        int maxlength = 20;        
        List<Keys> pressedKeys = new List<Keys>();
        Keys[] allowKeys = new Keys[] { Keys.Q, Keys.R, Keys.E, Keys.T, Keys.Y, Keys.U, Keys.I, Keys.O, Keys.P, Keys.A, Keys.S,
                Keys.D, Keys.F, Keys.G, Keys.H, Keys.J, Keys.K, Keys.L, Keys.Z, Keys.X, Keys.C, Keys.V, Keys.B, Keys.N, Keys.M,
                Keys.W};
        SpriteFont font;
        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        public EditTextSprite(Texture2D normalBackground, Vector2 position, Point frameSize,  float scale, Game game)
            : base(normalBackground, position, frameSize,
            0, new Point(1,1), Vector2.Zero,null, scale, game)
        {
            pointer = new Pointer(game, Pointer.TYPE.LINE, position + new Vector2(15,10));
            font = game.Content.Load<SpriteFont>(@"Fonts\Score");
        }

        public override void Update(GameTime gameTime, Rectangle clientBounds)
        {
            
            catchKey(gameTime);                     
            pointer.Sprite.Update(gameTime, clientBounds);
            
            base.Update(gameTime, clientBounds);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
           
            base.Draw(gameTime, spriteBatch);
            if (this.Focus == true)
            {
                pointer.Sprite.Draw(gameTime, spriteBatch);
            }
            
            spriteBatch.DrawString(font, value, position + new Vector2(15, 10), Color.White);
        }
        private void catchKey(GameTime gameTime)
        {
            if (value.Length < maxlength)
            {
                for (int i = 0; i < Keyboard.GetState().GetPressedKeys().GetLength(0); i++)
                {
                    pressedKeys.Add(Keyboard.GetState().GetPressedKeys().ElementAt(i));
                }
                
                for (int i = 0; i < pressedKeys.Count; i++)
                {
                    Keys keys = pressedKeys.ElementAt(i);
                    if (Keyboard.GetState().IsKeyUp(keys) )
                    {
                        if (keys.Equals(Keys.Back))
                        {
                            value = value.Substring(0,value.Length-1);
                            pointer.Sprite.Position -= new Vector2(11, 0);
                        }
                        if (keys.Equals(Keys.OemMinus)&& (Keyboard.GetState().IsKeyDown(Keys.LeftShift) ||
                        Keyboard.GetState().IsKeyDown(Keys.RightShift)))
                        {
                            value += "_";
                            pointer.Sprite.Position += new Vector2(11, 0);
                        }
                        if (keys.Equals(Keys.Space))
                        {
                            value += " ";
                            pointer.Sprite.Position += new Vector2(11, 0);
                        }
                        if (allowKeys.Contains(keys))
                        {
                            value += keys.ToString();
                            pointer.Sprite.Position += new Vector2(11, 0);
                        }
                        pressedKeys.Clear();
                    }
                }
            }
            else
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Back) )
                {                        
                    value = value.Substring(0,value.Length-1);
                    pointer.Sprite.Position -= new Vector2(11, 0);
                    pressedKeys.Clear();
                }
            }
            
            
        }
    }
}
